import { httpRequest } from "../httpRequest"
import type { IRes } from "../types"

export interface LoginRequest {
    username?: string;
    phone?: string;
    password: string;
}

export interface LoginResponse {
    user: {
        id: string;
        username: string;
        phone: string;
        role: string;
        status: string;
        create_time: Date;
    };
    token: string;
    expires_in: number;
}

export interface RegisterRequest {
    username: string;
    password: string;
    phone: string;
}

export const authApi = {
    login(data: LoginRequest): IRes<LoginResponse> {
        return httpRequest.post("/api/v1/auth/login", data)
    },
    register(data: RegisterRequest): IRes<LoginResponse> {
        return httpRequest.post("/api/v1/auth/register", data)
    },
    logout(): IRes<null> {
        return httpRequest.post("/api/v1/auth/logout")
    },
    refreshToken(token: string): IRes<{ token: string; expires_in: number }> {
        return httpRequest.post("/api/v1/auth/refresh", { token })
    },
    changePassword(data: {
        userId: string;
        oldPassword: string;
        newPassword: string;
    }): IRes<null> {
        return httpRequest.post("/api/v1/auth/change-password", data)
    }
}